package cn.cytong.nk0713.utils;

import org.nutz.dao.Chain;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 保存操作员操作日志
 */
public class OperatorServiceLogWriter {
    //增加年卡用户记录
    public static void saveOperatorLog(String note, Record operator, Record customer, Long serviceType, NutDao dao) {
        saveOperatorLog(note, operator, null, customer, serviceType, null, null, dao);
    }

    //年卡激活记录
    public static void saveOperatorLog(String note, Record operator, Record card, Long serviceType, BigDecimal fee, NutDao dao) {
        saveOperatorLog(note, operator, card, null, serviceType, fee, null, dao);
    }

    //年卡写卡记录
    public static void saveOperatorLog(String note, Record operator, Record card,
                                       Record customer, Long serviceType, NutDao dao) {
        saveOperatorLog(note, operator, card, customer, serviceType, null, null, dao);
    }

    public static void saveOperatorLog(String note, Record operator, Record card, Record customer,
                                       Long serviceType, BigDecimal fee, NutDao dao) {
        saveOperatorLog(note, operator, card, customer, serviceType, fee, null, dao);
    }

    public static void saveOperatorLog(String note, Record operator, Record card, Record customer,
                                       Long serviceType, BigDecimal fee, Integer deductType, NutDao dao) {
        Long customer_id = null;
        if (customer != null) customer_id = Long.valueOf(customer.getString("id"));

        Long card_id = null;
        if (card != null)
            card_id = Long.valueOf(card.getString("id"));

        Chain chain = Chain.make("operator_id", Long.valueOf(operator.getString("id")))
                .add("card_id", card_id)
                .add("customer_id", customer_id)
                .add("serviceType", serviceType)
                .add("fee", fee)
                .add("serviceAt", new Date())
                .add("note", note)
                .add("deductType", deductType);

        if (operator.getString("serviceBranch_id") != null) {
            chain = chain.add("branch_id", Long.valueOf(operator.getString("serviceBranch_id")));
        }

        if (operator.getString("serviceProvider_id") != null) {
            chain = chain.add("provider_id", Long.valueOf(operator.getString("serviceProvider_id")));
        }

        dao.insert("OperatorServiceLog", chain);
    }

}
